home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / sgcapture2disk / src / mysgcontroller / sgcontrolpanel.java
Encoding:
Java Source  |  2000-06-23  |  6.2 KB  |  228 lines

  1. /*
  2.  * QuickTime for Java SDK Sample Code
  3.  
  4.    Usage subject to restrictions in SDK License Agreement
  5.  * Copyright: © 1996-1999 Apple Computer, Inc.
  6.  
  7.  */
  8. package MySGController;
  9.  
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import java.io.*;
  13.  
  14. import quicktime.io.*;
  15. import quicktime.util.*;
  16. import quicktime.*;
  17. import quicktime.app.*;
  18. import quicktime.std.*;
  19. import quicktime.std.sg.*;
  20.  
  21. import quicktime.std.movies.*;
  22. import quicktime.std.movies.media.*;
  23. import quicktime.std.StdQTConstants;
  24. import quicktime.app.display.QTCanvas;
  25. import quicktime.app.image.*;
  26. import quicktime.qd.*;
  27. import quicktime.app.sg.SGDrawer;
  28.  
  29. import MyPlayMovie.PlayMovie;
  30.  
  31.  
  32. public class SGControlPanel extends Panel implements Runnable, Errors, StdQTConstants {
  33.  
  34. //_________________________ INSTANCE VARIABLES
  35.     public     Button             powerOn       = new Button("Power");    
  36.     public     Button             recordButton  = new Button("Record");
  37.     public     Button             stopButton    = new Button("Stop");
  38.     
  39.     public    QDGraphics         theGraphics;
  40.     public  SequenceGrabber mGrabber = null;
  41.     public     QTCanvas         theQTCanvas;
  42.     public  SGVideoChannel     mVideo;
  43.     public  SGDrawer        mDrawer;
  44.     public  QTFile            mFile;
  45.     public  boolean          isPowerOn = false;
  46.  
  47.     static final int kWidth = 320;
  48.     static final int kHeight = 240;
  49.  
  50. //_________________________ CLASS METHODS
  51.     
  52.     public void showMovie() {
  53.         PlayMovie pm = new PlayMovie("Captured Movie",mFile);
  54.         pm.show();
  55.         pm.toFront();
  56.     }
  57.  
  58.  
  59.     public void createNewMovie() {
  60.         try {
  61.                 Container c = getParent();
  62.                 for (;;) {
  63.                     if (c instanceof Frame) break;
  64.                     c = c.getParent();
  65.                 }
  66.  
  67.  
  68.                 FileDialog fd = new FileDialog ((Frame) c, "Save Movie As...", FileDialog.SAVE);
  69.                 fd.show();
  70.                 
  71.                 if(fd.getFile() == null)
  72.                     throw new QTIOException (userCanceledErr, "");
  73.                     
  74.                 mFile = new QTFile(fd.getDirectory() + fd.getFile());
  75.                 
  76.                 }
  77.                 catch (QTException qte) {
  78.                     System.out.println("Error in makeMovie: ");
  79.                     qte.printStackTrace(); 
  80.                 }
  81.     }
  82.     
  83.     
  84. // See the Comments below under the recordButton action listner to see why this run method is here.
  85. // Basically this is here as a workaround for using the SequenceGrabber on Windows Systems.
  86.  
  87.     public void run () {
  88.         try {
  89.             Thread.sleep(10);
  90.             mDrawer.startTasking();
  91.         } catch (Exception e) {
  92.              e.printStackTrace();
  93.         }
  94.     }
  95.  
  96.  
  97.     public void powerUp() throws QTException {
  98.             
  99.             QDRect bounds = new QDRect(kWidth, kHeight);
  100.                     
  101.             mGrabber = new SequenceGrabber();
  102.                             
  103.             mVideo = new SGVideoChannel(mGrabber);
  104.             //Bring up the sequence Grabber settings Dialog and make sure 
  105.             //you are receiving a clear image in the preview dialog.
  106.             mVideo.settingsDialog ();
  107.  
  108.             mVideo.setBounds (new QDRect(kWidth, kHeight));
  109.             mVideo.setUsage (seqGrabPreview | seqGrabRecord | seqGrabPlayDuringRecord);
  110.             
  111.             mGrabber.prepare(true,true);                
  112.             mDrawer = new SGDrawer(mVideo);
  113.             theQTCanvas.setClient(mDrawer,true);    //Uncomment to have the sequence grabber draw in the window                    
  114.             mGrabber.startPreview();    
  115.             
  116.             isPowerOn = true;
  117.     }
  118.     
  119.     
  120.     public void doAction(int what) {
  121.        try {
  122.             mGrabber.pause(what);
  123.         }
  124.         
  125.         catch (QTException e) {
  126.             e.printStackTrace();
  127.         }
  128.         
  129.     }
  130.     
  131.     public SGControlPanel(QTCanvas myQTCanvas) throws QTException {    
  132.  
  133.         theQTCanvas = myQTCanvas;
  134.                 
  135.         setLayout(new FlowLayout());
  136.         setBackground(getBackground());
  137.         setLayout(new FlowLayout());
  138.         
  139.  
  140.         add (powerOn);
  141.         add (recordButton);
  142.         add (stopButton);
  143.         
  144.         QTRuntimeException.registerHandler (new Handler());
  145.  
  146.  
  147.         powerOn.addActionListener (new ActionListener () {
  148.             public void actionPerformed (ActionEvent event) {
  149.  
  150.                 try {
  151.                     powerUp();
  152.                 } catch (QTException err) {
  153.                     err.printStackTrace();
  154.                 }
  155.                 System.out.println("Powered UP");
  156.             }
  157.         });
  158.         
  159.         recordButton.addActionListener (new ActionListener () {
  160.             public void actionPerformed (ActionEvent event) {
  161.                 try{
  162.                 
  163.                     if (isPowerOn == false) {
  164.                         System.out.println("You Must first Click The Power button");
  165.                         return;
  166.                     }
  167.                 
  168.                       mDrawer.stopTasking();
  169.                       mGrabber.stop();
  170.                     createNewMovie();
  171.                 
  172.                     if (mFile != null) {
  173.                         mGrabber.setDataOutput(mFile,seqGrabToDisk);
  174.                         mGrabber.startRecord();
  175.                         
  176.                         //This code of spawning a new thread is here as a workaround for MS Windows 98 Systems.
  177.                         //It appears as though a race condition could occur where the setDataOutput does not fully complete
  178.                         //execution before the task object for the sequence grabber would try to idle it.
  179.                         //As a result of the race condition a -9402 exception (cantDoThatInCurrentMode) would be thrown.
  180.                         //-9402 in some instances means that we are trying to set a parameter during a record operation
  181.                         //This is a harmless error, but because a runtime exception gets thrown, the task object
  182.                         //for the SGDrawer (which is responsible for calling SGIdle() internally )  would terminate the objects
  183.                         //tasker. Starting a new thread which simply starts the SGDrawer object idling seems to fix this problem.
  184.                         //
  185.                         new Thread (SGControlPanel.this).start();            
  186.                     }
  187.                 } catch (QTException ee){
  188.                     ee.printStackTrace();
  189.                 }    
  190.             }
  191.         });        
  192.  
  193.         stopButton.addActionListener (new ActionListener () {
  194.             public void actionPerformed (ActionEvent event) {
  195.                 try {
  196.                 
  197.                     if (isPowerOn == false) {
  198.                         System.out.println("You Must first Click the Power button");
  199.                         return;
  200.                     }
  201.                       mDrawer.stopTasking();
  202.                     mGrabber.stop();
  203.                 System.out.println("*** BEFORE START PREVIEW ***");
  204.                       mGrabber.startPreview();
  205.                       mDrawer.startTasking();
  206.                       
  207.                       showMovie();
  208.  
  209.                 } catch (QTException ee) {
  210.                     ee.printStackTrace();
  211.                 }
  212.             }
  213.         });        
  214.  
  215.     }
  216.  
  217.     //_________________________ Runtime Error Handling
  218.     static class Handler implements QTRuntimeHandler {
  219.         public void exceptionOccurred (QTRuntimeException e, Object eGenerator, String methodNameIfKnown, boolean unrecoverableFlag) {
  220.             System.out.println (eGenerator + "," + methodNameIfKnown + ",unrecoverable=" + unrecoverableFlag);
  221.             e.printStackTrace();
  222.             throw e;    // we don't handle this exception - just print stack trace and throw it
  223.         }
  224.     }
  225.  
  226.  
  227. }
  228.